<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Session;
use \think\Db;
class Formuser extends Common
{
	public function add(){
		return $this->fetch();
	}

	public function edit($id){
		$result = Db::name("users")->where('u_id',$id)->find();
    	$this->assign("result",$result);
    	return $this->fetch();
	}
public function jiekou(){

        $rs=Db::name('users')->select();
        return json($rs);
}
	public function addUser(){
                // dump(input('param.u_name'));exit();
    
		$info = '';
       	if (request()->isPost()) {
       		//创建数组，用video表的所有字段赋值
          $result = Db::name('users')->where('u_name',input('param.u_name'))->find();
          $result2 = Db::name('users')->where('u_telephone',input('param.u_telephone'))->find();
          if($result){
             $this->error("账号重复",'form/add');
          }
          if($result2){
          	$this->error("联系方式重复",'form/add');
          }
          $file = request()->file('u_pic');
          $data=array();
          if($file){
              $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters');
              $data=[
                  'u_name'=>input('param.u_name'),
                  'u_password'=>md5(input('param.u_password')),
                  'u_gender'=>input('param.u_gender'),
                  'u_telephone'=>input('param.u_telephone'),
                  'u_num'=>input('param.u_num'),
                  'u_pic'=>$info->getFilename(),
       		];
          }else{
          	$data=[
                  'u_name'=>input('param.u_name'),
                  'u_password'=>md5(input('param.u_password')),
                  'u_gender'=>input('param.u_gender'),
                  'u_telephone'=>input('param.u_telephone'),
                  'u_num'=>input('param.u_num'),
                  'u_pic'=>'default.jpg',
       		];
          }
        $res=Db::name('users')->insert($data);
        
        //处理结果
        if ($res) {
        	$this->success("添加用户成功",'user/index');
        }
        else {
        	$this->error("添加用户失败",'user/index');
        }
       	}
	}
	
	public function updateuser($id){
		 $info = '';
        if (request()->isPost()) {
          //创建数组，用video表的所有字段赋值
          $result1 = Db::name('users')->where('u_telephone',input('param.u_telephone'))->find();
          if($result1&&$result1['u_id']!=$id){
          	$this->error("联系方式重复",'user/index');
          }
          $file = request()->file('pic');
          $pic="";
          $pic2=input('pic2');//图片没改之前的名字
          $data=array();
          if($file){
              $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters');
              if($info){
                $pic = $info->getFilename();
                 if(input('param.u_password')==''){
          $data=[
                  'u_name'=>input('param.u_name'),
                  'u_gender'=>input('param.u_gender'),
                  'u_telephone'=>input('param.u_telephone'),
                  'u_num'=>input('param.u_num'),
                  'u_pic'=>$info->getFilename(),
          ];
          
        }else{
          $data=[
                  'u_name'=>input('param.u_name'),
                  'u_password'=>md5(input('param.u_password')),
                  'u_gender'=>input('param.u_gender'),
                  'u_telephone'=>input('param.u_telephone'),
                  'u_num'=>input('param.u_num'),
                  'u_pic'=>$info->getFilename(),
          ];
          var_dump($data);
        }
              }else{
                echo $file->getError();
              }
          }
          else{
          if(input('param.u_password')==''){
          $data=[
                  'u_name'=>input('param.u_name'),
                  'u_gender'=>input('param.u_gender'),
                  'u_telephone'=>input('param.u_telephone'),
                  'u_num'=>input('param.u_num'),
          ];
        }else{
          $data=[
                  'u_name'=>input('param.u_name'),
                  'u_password'=>md5(input('param.u_password')),
                  'u_gender'=>input('param.u_gender'),
                  'u_telephone'=>input('param.u_telephone'),
                  'u_num'=>input('param.u_num'),
          ];
        }
}
          
		  
          if(!empty($pic)&&$pic2!='default.jpg'){
            if(file_exists(ROOT_PATH . 'public' . DS . 'static'. DS .'posters'.DS.$pic2)){
              unlink(ROOT_PATH . 'public' . DS . 'static'. DS .'posters'.DS.$pic2);
            }
          }
          
         
        $res=Db::name('users')->where("u_id",$id)->update($data);
        //处理结果
        if ($res!==false) {
          $this->success("修改用户成功",'user/index');
        }
        else {
          $this->error("修改用户失败",'user/index');
        }
      }
	}

	public function delUser($id){
		if (Db::name("users")->delete($id)) {
      $this->success('删除成功','user/index');
    }else 
    $this->error('删除失败','user/index');
	}
}